/**
 * 
 */
package com.example.a.web;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * 假设我们有一个提供计算功能的微服务模块，我们实现一个RESTful API，通过传入两个参数a和b，最后返回a + b的结果。
 */
@RestController
public class ComputeController {
	private final Logger logger = LoggerFactory.getLogger(getClass());

	@Resource
	private DiscoveryClient client;

	@GetMapping(value = "/add")
	public String add(@RequestParam Integer a, @RequestParam Integer b) {
		ServiceInstance instance = client.getLocalServiceInstance();
		Integer r = a + b;
		logger.info("/add, host:{}, service_id:{}, result:{}", instance.getHost(), instance.getServiceId(), r);
		return "From Service-A, Result is " + r;
	}
}
